#ifndef xpack_lang_cxx_index_of_suffix
#define xpack_lang_cxx_index_of_suffix
#pragma push_macro("xuser")
#undef  xuser
#define xuser mixc::lang_cxx::inc
#include"define/base_type.hpp"
#include"lang/cxx.hpp"
#include"macro/xforward.hpp"
#pragma pop_macro("xuser")

namespace mixc::lang_cxx{
    inline uxx index_of_suffix_core(auto main, auto pattern, auto const & compare){
        if (uxx main_length = main->length; main_length == 0){
            return not_exist;
        }
        else for(uxx i = 0, length = pattern->length; i < length; i++){
            // 约定:
            // left 为待匹配字符串的字符，right 为匹配参数字符
            if (compare(main[main_length - 1], pattern[i]) == 0){
                return i;
            }
        }
        return not_exist;
    }

    inline uxx index_of_suffix_core2(auto main, auto pattern, auto const & compare){
        if (uxx main_length = main->length; main_length == 0){
            return not_exist;
        }
        else for(uxx i = 0, length = pattern->length; i < length; i++){
            if (uxx item_length = pattern[i]->length; item_length > main_length){
                continue;
            }
            else if (auto suffix = main.backward(main_length - item_length); pattern[i].compare(suffix, compare) == 0){
                return i;
            }
        }
        return not_exist;
    }

    template<class item_t>
    template<class item_seq_t, class>
    inline uxx cxx<item_t>::index_of_suffix(item_seq_t const & pattern, invoke_compare_t compare) const {
        return index_of_suffix_core(xthe, inc::unified_seq<item_seq_t>(pattern), compare);
    }

    template<class item_t>
    template<class seq_t, class, class>
    inline uxx cxx<item_t>::index_of_suffix(seq_t && pattern, invoke_compare_t compare) const {
        return index_of_suffix_core2(xthe, inc::unified_seq<seq_t>(xforward(pattern)), compare);
    }
}

#endif
